package com.wuzuqing.android.eightdiagrams.term

import java.util.*
import kotlin.math.abs

object Bazhi {

    private  var tiangan = arrayOf("癸", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬")
    private  var dizhi = arrayOf("亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌")
    private   val dzArr = arrayOf("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥")

    // 1911年月干
    private   var yuegan1911 = arrayOf("庚", "辛", "壬", "癸", "甲", "乙", "丙", "丁", "戊", "己")
    private  var jie = arrayOf("立春", "惊蛰", "清明", "立夏", "芒种", "小暑", "立秋", "白露", "寒露", "立冬", "大雪", "XIAO_HAN", "LI_CHUN")
    private   var ssShorter = mapOf(
        "比肩" to "比",
        "劫财" to "劫",
        "正印" to "印",
        "偏印" to "枭",
        "正官" to "官",
        "七杀" to "杀",
        "正财" to "财",
        "偏财" to "才",
        "伤官" to "伤",
        "食神" to "食",
    )
    private   val gzu = arrayOf(
        "甲子",
        "丙寅",
        "戊辰",
        "庚午",
        "壬申",
        "甲戌",
        "丙子",
        "戊寅",
        "庚辰",
        "壬午",
        "甲申",
        "丙戌",
        "戊子",
        "庚寅",
        "壬辰",
        "甲午",
        "丙申",
        "戊戌",
        "庚子",
        "壬寅",
        "甲辰",
        "丙午",
        "戊申",
        "庚戌",
        "壬子",
        "甲寅",
        "丙辰",
        "戊午",
        "庚申",
        "壬戌"
    )
    private  val zzu = arrayOf(
        "乙丑",
        "丁卯",
        "己巳",
        "辛未",
        "癸酉",
        "乙亥",
        "丁丑",
        "己卯",
        "辛巳",
        "癸未",
        "乙酉",
        "丁亥",
        "己丑",
        "辛卯",
        "癸巳",
        "乙未",
        "丁酉",
        "己亥",
        "辛丑",
        "癸卯",
        "乙巳",
        "丁未",
        "己酉",
        "辛亥",
        "癸丑",
        "乙卯",
        "丁巳",
        "己未",
        "辛酉",
        "癸亥"
    )
    private val nyzu = arrayOf(
        "海中金",
        "炉中火",
        "大林木",
        "路旁土",
        "剑锋金",
        "山头火",
        "涧下水",
        "城头土",
        "白腊金",
        "杨柳木 ",
        "泉中水",
        "屋上土",
        "霹雳火",
        "松柏木",
        "长流水",
        "砂石金",
        "山下火",
        "平地木",
        "壁上土",
        "金薄金",
        "覆灯火",
        "天河水",
        "大驿土",
        "钗环金",
        "桑柘木",
        "大溪水",
        "沙中土",
        "天上火",
        "石榴木",
        "大海水"
    )

    // 天干地支留意
    private  var liuyi = arrayOf(
        arrayOf("甲己", "合土"),
        arrayOf("乙庚", "合金"),
        arrayOf("丙辛", "合水"),
        arrayOf("丁壬", "合木"),
        arrayOf("戊癸", "合火"),

        arrayOf("甲庚", "冲"),
        arrayOf("乙辛", "冲"),
        arrayOf("丙壬", "冲"),
        arrayOf("丁癸", "冲"),

        arrayOf("巳申", "合化水"),
        arrayOf("辰酉", "合化金"),
        arrayOf("卯戌", "合化火"),
        arrayOf("寅亥", "合化木"),
        arrayOf("子丑", "合化土"),
        arrayOf("午未", "合化火或土"),

        arrayOf("申子辰", "合化水"),
        arrayOf("寅午戌", "合化火"),
        arrayOf("亥卯未", "合化木"),
        arrayOf("巳酉丑", "合化金"),

        arrayOf("亥子丑", "汇聚北方水"),
        arrayOf("寅卯辰", "汇聚东方木"),
        arrayOf("巳午未", "汇聚南方火"),
        arrayOf("申酉戌", "汇聚西方金"),

        arrayOf("子卯", "为无礼之刑"),
        arrayOf("丑未戌", "为恃势之刑"),
        arrayOf("寅巳申", "为无恩之刑"),
        arrayOf("辰辰", "为自刑"),
        arrayOf("午午", "为自刑"),
        arrayOf("酉酉", "为自刑"),
        arrayOf("亥亥", "为自刑"),

        arrayOf("子午", "相冲"),
        arrayOf("卯酉", "相冲"),
        arrayOf("寅申", "相冲"),
        arrayOf("巳亥", "相冲"),
        arrayOf("辰戌", "相冲"),
        arrayOf("丑未", "相冲"),

        arrayOf("子未", "相害"),
        arrayOf("丑午", "相害"),
        arrayOf("寅巳", "相害"),
        arrayOf("卯辰", "相害"),
        arrayOf("申亥", "相害"),
        arrayOf("酉戍", "相害"),

        arrayOf("寅午", "暗合土"),
        arrayOf("子巳", "暗合火"),
        arrayOf("巳酉", "暗合水"),
        arrayOf("卯申", "暗合金"),
        arrayOf("亥午", "暗合木"),

        arrayOf("子酉", "相破"),
        arrayOf("寅亥", "相破"),
        arrayOf("卯午", "相破"),
        arrayOf("辰丑", "相破"),
        arrayOf("巳申", "相破"),
        arrayOf("未戌", "相破")
    )


    private var currentYear: Int
    private var currentYearMonth: String
    private var currentDay: Int

    init {
        val calendar = Calendar.getInstance()
        currentYear = calendar.get(Calendar.YEAR)
        currentYearMonth = "$currentYear-${(calendar.get(Calendar.MONTH) + 1)}"
        currentDay = calendar.get(Calendar.DATE)
    }


    // 本命部分的天干留意
    fun getLiuYi(tglineArr:MutableList<String>,dzlineArr:MutableList<String>):MutableList<MutableList<String>> {
        val tgLiuYiList = mutableListOf<String>()
        val dzLiuYiList = mutableListOf<String>()
        for (item in liuyi) {
            // 天干留意
            var tmpArr = tglineArr.toMutableList()
            var result = ""
            for (j in item.indices) {
                val iArr = tmpArr.indexOf(item[0][j].toString())
                if (iArr >= 0)
                    result += tmpArr.removeAt(iArr)[0]
            }
            if (result == item[0]) {
                tgLiuYiList.add("${item[0]}${item[1]}")
            }
            // 地支留意
            result = ""
            tmpArr = dzlineArr.toMutableList()
            for (j in 0 until  item[0].length) {
                val iArr = tmpArr.indexOf(item[0][j].toString())
                if (iArr >= 0)
                    result += tmpArr.removeAt(iArr)[0]
            }
            if (result == item[0]) {
                dzLiuYiList.add("${item[0]}${item[1]}")
            }
        }
        return mutableListOf(tgLiuYiList,dzLiuYiList)
    }
    private   val xkarr = arrayOf(
        arrayOf("甲子", "乙丑", "丙寅", "丁卯", "戊辰", "己巳", "庚午", "辛未", "壬申", "癸酉", "戌亥"),
        arrayOf("甲戌", "乙亥", "丙子", "丁丑", "戊寅", "己卯", "庚辰", "辛巳", "壬午", "癸未", "申酉"),
        arrayOf("甲申", "乙酉", "丙戌", "丁亥", "戊子", "己丑", "庚寅", "辛卯", "壬辰", "癸巳", "午未"),
        arrayOf("甲午", "乙未", "丙申", "丁酉", "戊戌", "己亥", "庚子", "辛丑", "壬寅", "癸卯", "辰巳"),
        arrayOf("甲辰", "乙巳", "丙午", "丁未", "戊申", "己酉", "庚戌", "辛亥", "壬子", "癸丑", "寅卯"),
        arrayOf("甲寅", "乙卯", "丙辰", "丁巳", "戊午", "己未", "庚申", "辛酉", "壬戌", "癸亥", "子丑")
    )

    // 计算空亡
    fun getXunKong(gz1: String): String {
        var tag = 0
        var xunk = ""
        for (i in 0 until 5) {
            for (j in 0 until 10) {
                if (xkarr[i][j] == gz1) {
                    xunk = xkarr[i][10]
                    tag = 1

                }
            }
            if (tag == 1) break
        }
        return xunk
    }

    //获取五行
    fun getWuxing(tgdz: String): String {
        var wx = ""
        when (tgdz) {
            "子" -> wx = "水"
            "亥" -> wx = "水"
            "寅" -> wx = "木"
            "卯" -> wx = "木"
            "巳" -> wx = "火"
            "午" -> wx = "火"
            "申" -> wx = "金"
            "酉" -> wx = "金"
            "辰" -> wx = "土"
            "戌" -> wx = "土"
            "丑" -> wx = "土"
            "未" -> wx = "土"
            "甲" -> wx = "木"
            "乙" -> wx = "木"
            "丙" -> wx = "火"
            "丁" -> wx = "火"
            "戊" -> wx = "土"
            "己" -> wx = "土"
            "庚" -> wx = "金"
            "辛" -> wx = "金"
            "壬" -> wx = "水"
            "癸" -> wx = "水"
        }
        return wx
    }

    //获取纳音图
    fun getNayin(gz: String): String {
        val z1 = gzu.indexOf(gz)
        var nayin = ""
        nayin = if (z1 != -1) {
            nyzu[z1]
        } else {
            nyzu[zzu.indexOf(gz)]
        }
        return nayin
    }

    private val nianGanShenShaArr = arrayOf(
        arrayOf("天乙贵人", "甲戊:丑未", "乙己:申子", "丙丁:亥酉", "壬癸:卯巳", "庚辛:寅午"),
        arrayOf("太极贵人", "甲乙:子午", "丙丁:卯酉", "戊己:辰戌丑未", "庚辛:寅亥", "壬癸:巳申"),
        arrayOf("文昌贵人", "甲乙:巳午", "丙戊:申", "丁己:酉", "庚:亥", "辛:子", "壬:寅", "癸:卯"),
        arrayOf("天厨贵人", "甲:巳", "乙:午", "丙:子", "丁:巳", "戊:午", "己:申", "庚:寅", "辛:午", "壬:酉", "癸:亥"),
        arrayOf("国印贵人", "甲:戌", "乙:亥", "丙:丑", "丁:寅", "戊:丑", "己:寅", "庚:辰", "辛:巳", "壬:未", "癸:申"),
        arrayOf("学堂", "甲:己亥", "乙:壬午", "丙:丙寅", "丁:丁酉", "戊:戊寅", "己:己酉", "庚:辛巳", "辛:甲子", "壬:甲申", "癸:乙卯"),
        arrayOf("词馆", "甲:庚寅", "乙:辛卯", "丙:乙巳", "丁:戊午", "戊:丁巳", "己:庚午", "庚:壬申", "辛:癸酉", "壬:癸亥", "癸:壬戌")
    )

    //获取年干神煞
    fun getNianGanShenSha(ng: String, gz: String): String {
        val arr = nianGanShenShaArr
        var shens = ""
        var str: List<String>
        val dza = gz.substring(1)
        for (i in arr.indices) {
            if (i <= 4) {
                for (j in 1 until arr[i].size) {
                    str = arr[i][j].split(':')
                    if (str[0].contains(ng) && str[1].contains(dza)) {
                        shens = shens + " " + arr[i][0]
                        break
                    }
                }
            } else {
                for (j in 1 until arr[i].size) {
                    str = arr[i][j].split(':')
                    if (str[0] == ng && str[1] == gz && getWuxing(ng) == getNayin(gz).substring(2)) {
                        shens = shens + " " + arr[i][0]
                        break
                    }
                }
            }
        }
        return (shens)
    }

    private var nianZhiShenSha = arrayOf(
        arrayOf("红鸾", "子:卯", "丑:寅", "寅:丑", "卯:子", "辰:亥", "巳:戌", "午:酉", "未:申", "申:未", "酉:午", "戌:巳", "亥:辰"),
        arrayOf("天喜", "子:酉", "丑:申", "寅:未", "卯:午", "辰:巳", "巳:辰", "午:卯", "未:寅", "申:丑", "酉:子", "戌:亥", "亥:戌"),
        arrayOf("元辰", "子:未", "丑:申", "寅:酉", "卯:戌", "辰:亥", "巳:子", "午:丑", "未:寅", "申:卯", "酉:辰", "戌:巳", "亥:午"),
        arrayOf("元辰", "子:巳", "丑:午", "寅:未", "卯:申", "辰:酉", "巳:戌", "午:亥", "未:子", "申:丑", "酉:寅", "戌:卯", "亥:辰"),
        arrayOf("灾煞", "申子辰:午", "亥卯未:酉", "寅午戌:子", "巳酉丑:卯"),
        arrayOf("孤辰", "亥子丑:寅", "寅卯辰:巳", "巳午未:申", "申酉戌:亥"),
        arrayOf("寡宿", "亥子丑:戌", "寅卯辰:丑", "巳午未:辰", "申酉戌:未"),
        arrayOf("驿马", "申子辰:寅", "寅午戌:申", "巳酉丑:亥", "亥卯未:巳"),
        arrayOf("华盖", "寅午戌:戌", "亥卯未:未", "申子辰:辰", "巳酉丑:丑"),
        arrayOf("将星", "寅午戌:午", "巳酉丑:酉", "申子辰:子", "亥卯未:卯"),
        arrayOf("劫煞", "申子辰:巳", "亥卯未:申", "寅午戌:亥", "巳酉丑:寅"),
        arrayOf("桃花", "申子辰:酉", "寅午戌:卯", "巳酉丑:午", "亥卯未:子"),
        arrayOf("亡神", "寅午戌:巳", "亥卯未:寅", "巳酉丑:申", "申子辰:亥"),
        arrayOf("天罗地网", "辰:巳", "巳:辰", "戌:亥", "亥:戌")
    )

    /**
     * @param       nz  年支
     * @param       dza 目标八字
     * @param       nzIndex 年支索引
     * @param       sex  性别  1男 0女
     */
    fun getNianZhiShen(nz: String, dza: String, nzIndex: Int, sex: Int): String {
        val arr = nianZhiShenSha
        var shens = ""
        var str: List<String>
        for (i in arr.indices) {
            if (i == 2 && sex == 1) continue
            if (i == 3 && sex == 0) continue
            for (j in 1 until arr[i].size) {
                str = arr[i][j].split(":")
                if (str[0].contains(nz) && str[1] == dza) {
                    shens = shens + " " + arr[i][0]
                    break
                }
            }
        }
        val pmz = (nzIndex + 3) % 12
        val dkz = (nzIndex + 2) % 12
        val smz = (nzIndex + 10) % 12
        if (dzArr[pmz] == dza) {
            shens += " 披麻"
        }
        if (dzArr[dkz] == dza) {
            shens += " 吊客"
        }
        if (dzArr[smz] == dza) {
            shens += " 丧门"
        }
        return (shens)
    }
    private var gouJiaoShenSha = arrayOf(
        arrayOf("勾煞","子酉1","子卯0","丑戊1","丑辰0","寅亥1","寅巳0","卯子1","卯午0","辰丑1","辰未0",
            "巳寅1","巳申0","午卯1","午酉1","未辰1","未戌0","申巳1","申亥0",
            "酉午1","酉子0","戌未1","戌丑0","亥申1","亥寅0"),
        arrayOf("绞煞","子酉0","子卯1","丑戊0","丑辰1","寅亥0","寅巳1","卯子0","卯午1","辰丑0","辰未1",
            "巳寅0","巳申1","午卯0","午酉1","未辰0","未戌1","申巳0","申亥1",
            "酉午0","酉子1","戌未0","戌丑1","亥申0","亥寅1")
    )

    fun getNianZhiOther(ngz: String, dz: String): String {
        val isYang = if (LunarConstant.GAN.indexOf(ngz[0].toString()) % 2 == 0) 1 else 0
        val key = "${ngz[1]}${dz}$isYang"
        gouJiaoShenSha.forEach {
            for (i in 1 until it.size) {
                if (key == it[i]) {
                    return it[0]
                }
            }
        }
        return "";
    }
    //寅午戌月在辛，申子辰月在丁，亥卯未月在己，巳酉丑月在乙
    private var yueZhiSheSha = arrayOf(
        arrayOf("天德贵人", "寅:丁", "卯:申", "辰:壬", "巳:辛", "午:亥", "未:甲", "申:癸", "酉:寅", "戌:丙", "亥:乙", "子:己", "丑:庚"),
        arrayOf("天德合", "寅:壬", "卯:巳", "辰:丁", "巳:丙", "午:寅", "未:己", "申:戊", "酉:亥", "戌:辛", "亥:庚", "子:申", "丑:乙"),
        //正寅月見壬、二卯月見巳、三辰月見丁、四巳月見丙、五午月見寅、六未月見己、七申月見戊、八酉月見亥、九戌月見辛、十亥月見庚、十一子月見申、十二丑月見乙

        arrayOf("月德贵人", "寅午戌:丙", "申子辰:壬", "亥卯未:甲", "巳酉丑:庚"),
        arrayOf("月德合", "寅午戌:辛", "申子辰:丁", "亥卯未:己", "巳酉丑:乙"),
        arrayOf("天医", "寅:丑", "卯:寅", "辰:卯", "巳:辰", "午:巳", "未:午", "申:未", "酉:申", "戌:酉", "亥:戌", "子:亥", "丑:子"),
        //子月生人见地支午。
        //丑月生人见地支子。
        //寅月生人见地支丑。
        //卯月生人见地支未。
        //辰月生人见地支寅。
        //巳月生人见地支申。
        //午月生人见地支卵。
        //未月生人见地支西。
        //申月生人见地支辰。
        //酉月生人见地支成。
        //戌月生人见地支已。
        //亥月生人见地支亥
        arrayOf("血刃", "寅:丑", "卯:未", "辰:寅", "巳:申", "午:卯", "未:酉", "申:辰", "酉:戌", "戌:巳", "亥:亥", "子:午", "丑:子"),
        arrayOf("童子煞", "寅卯辰申酉戌:子寅", "亥子丑巳午未:卯未辰")
    )


    fun getYueZhiShenSha(yzx: String, tgx: String, dzy: String,type:Int): String {
        val arr = yueZhiSheSha
        var shens = ""
        var str: List<String>
        for (i in arr.indices) {
            for (j in 1 until arr[i].size) {
                str = arr[i][j].split(":")
                if (str[0].contains(yzx)) {
                    if (  (str[1] == tgx || str[1] == dzy)){
                        shens = shens + " " + arr[i][0]
                        break
                    }else if (str[0].length==6 && str[1].contains(dzy) && type == 1){
                        shens = shens + " " + arr[i][0]
                        break
                    }
                }
            }
        }
        return (shens)
    }

    private var riGanShenSha = arrayOf(
        arrayOf("天乙贵人", "甲戊:丑未", "乙己:申子", "丙丁:亥酉", "壬癸:卯巳", "庚辛:寅午"),
        arrayOf("太极贵人", "甲乙:子午", "丙丁:卯酉", "戊己:辰戌丑未", "庚辛:寅亥", "壬癸:巳申"),
        arrayOf("文昌贵人", "甲乙:巳午", "丙戊:申", "丁己:酉", "庚:亥", "辛:子", "壬:寅", "癸:卯"),
        arrayOf("金舆", "甲:辰", "乙:巳", "丙戊:未", "丁己:申", "庚:戌", "辛:亥", "壬:丑", "癸:寅"),
        arrayOf("禄神", "甲:寅", "乙:卯", "丙戊:巳", "丁己:午", "庚:申", "辛:酉", "壬:亥", "癸:子"),
        arrayOf("羊刃", "甲:卯", "乙:寅", "丙戊:午", "丁己:巳", "庚:酉", "辛:申", "壬:子", "癸:亥"),
        arrayOf("天厨贵人", "甲:巳", "乙:午", "丙:子", "丁:巳", "戊:午", "己:申", "庚:寅", "辛:午", "壬:酉", "癸:亥"),
        arrayOf("国印贵人", "甲:戌", "乙:亥", "丙:丑", "丁:寅", "戊:丑", "己:寅", "庚:辰", "辛:巳", "壬:未", "癸:申"),
        arrayOf("学堂", "甲:己亥", "乙:壬午", "丙:丙寅", "丁:丁酉", "戊:戊寅", "己:己酉", "庚:辛巳", "辛:甲子", "壬:甲申", "癸:乙卯"),
        arrayOf("词馆", "甲:庚寅", "乙:辛卯", "丙:乙巳", "丁:戊午", "戊:丁巳", "己:庚午", "庚:壬申", "辛:癸酉", "壬:癸亥", "癸:壬戌"),
        arrayOf("墓煞", "甲:庚戌庚辰", "乙:辛丑辛未", "丙:壬辰壬戌", "丁:癸丑癸未", "戊:甲辰甲戌", "己:乙丑乙未", "庚:丙辰丙戌", "辛:丁丑丁未", "壬:戊辰戊戌", "癸:己丑己未"),
        arrayOf("血刃", "甲:卯", "乙:辰", "丙:午", "丁:未", "戊:午","庚:酉", "辛:戌", "己:未"), //待定
        //甲龙乙蛇丙戊羊, 丁己猴歌庚犬方,
        //辛猪壬牛癸逢虎, 凡人遇此福气昌.

        //癸亥日癸丑时, 癸丑日癸亥时, 拱子禄.丁巳日丁未时, 己未日己巳时, 拱午禄.戊辰日戊午时, 拱巳禄.
        arrayOf("拱禄", "甲:卯", "乙:辰", "丙:午", "丁:未", "戊:午","庚:酉", "辛:戌", "己:未"), //待定
    )


    fun getRiGanShenSha(rg: String, gz: String): String {
        val arr = riGanShenSha
        var shens = ""
        val dza = gz[1].toString()
        var str: List<String>
        for (i in arr.indices) {
            if (i <= 8) {
                for (j in 1 until arr[i].size) {
                    str = arr[i][j].split(":")
                    if (str[0].contains(rg) && str[1].contains(dza)) {
                        shens = shens + " " + arr[i][0]
                    }
                }
            } else {
                for (j in 1 until arr[i].size) {
                    str = arr[i][j].split(":")
                    if (str[0] == rg && str[1] == gz && getWuxing(rg) == getNayin(gz).substring(2)) {
                        shens = shens + " " + arr[i][0]
                    } else if (str[0] == rg && (str[1] == gz || (str[1].length==4 && str[1].contains(gz)))) {
                        shens = shens + " " + arr[i][0]
                    }
                }
            }
        }
        return (shens)
    }

    private  var riZhiShenSha = arrayOf(
        arrayOf("驿马", "申子辰:寅", "寅午戌:申", "巳酉丑:亥", "亥卯未:巳"),
        arrayOf("华盖", "寅午戌:戌", "亥卯未:未", "申子辰:辰", "巳酉丑:丑"),
        arrayOf("将星", "寅午戌:午", "巳酉丑:酉", "申子辰:子", "亥卯未:卯"),
        arrayOf("亡神", "寅午戌:巳", "亥卯未:寅", "巳酉丑:申", "申子辰:亥"),
        arrayOf("劫煞", "申子辰:巳", "亥卯未:申", "寅午戌:亥", "巳酉丑:寅"),
        arrayOf("桃花", "申子辰:酉", "寅午戌:卯", "巳酉丑:午", "亥卯未:子"),
        arrayOf("天罗地网", "辰:巳", "巳:辰", "戌:亥", "亥:戌")
    )

    fun getRiZhiShenSha(rz: String, dz: String, rzx: Int): String {
        val arr = riZhiShenSha
        var shens = ""
        var str: List<String>
        for (i in arr.indices) {
            for (j in 1 until arr[i].size) {
                str = arr[i][j].split(":")
                if (str[0].contains(rz) && str[1] == dz) {
                    shens = shens + " " + arr[i][0]
                    break
                }
            }
        }
        val pmz = (rzx + 3) % 12
        val dkz = (rzx + 2) % 12
        val smz = (rzx + 10) % 12

        if (dzArr[pmz] == dz) {
            shens = shens + " 披麻"
        }
        if (dzArr[dkz] == dz) {
            shens = shens + " 吊客"
        }
        if (dzArr[smz] == dz) {
            shens = shens + " 丧门"
        }
        return (shens)
    }

    fun getShiShenStr(tg:String,rg:String):String{
        return getShishen(tiangan.indexOf(tg), tiangan.indexOf(rg))
    }
    fun getShiShenShort(tg:String,rg:String):String{
        return ssShorter[getShishen(tiangan.indexOf(tg), tiangan.indexOf(rg))].orEmpty()
    }
    /*
    * 获取十神
    * @param tgx 天干索引
    * @param rgx 日干索引
    */
    fun getShishen(tgx: Int, rgx: Int): String {
        val tianGanIndex = when (tgx) {
            0 -> 10
            else -> tgx
        }
        val riGanIndex = when (rgx) {
            0 -> 10
            else -> rgx
        }
        val cha = riGanIndex - tianGanIndex
        var str = ""
        if (cha >= 0) {
            when (cha) {
                0 -> str = "比肩"
                1 -> {
                    str = if (riGanIndex % 2 == 0) {
                        "劫财"
                    } else {
                        "正印"
                    }
                }

                2 -> str = "偏印"
                3 -> {
                    str = if (riGanIndex % 2 == 0) {
                        "正印"
                    } else {
                        "正官"
                    }
                }
                4 -> str = "七杀"
                5 -> {
                    str = if (riGanIndex % 2 == 0) {
                        "正官"
                    } else {
                        "正财"
                    }
                }
                6 -> str = "偏财"
                7 -> {
                    str = if (riGanIndex % 2 == 0) {
                        "正财"
                    } else {
                        "伤官"
                    }
                }
                8 -> str = "食神"
                9 -> str = "伤官"
            }
        } else {
            when (abs(cha)) {
                1 -> {
                    str = if (riGanIndex % 2 == 1) {
                        "劫财"
                    } else {
                        "伤官"
                    }
                }
                2 -> str = "食神"
                3 -> {
                    str = if (riGanIndex % 2 == 1) {
                        "伤官"
                    } else {
                        "正财"
                    }
                }
                4 -> str = "偏财"
                5 -> {
                    str = if (riGanIndex % 2 == 1) {
                        "正财"
                    } else {
                        "正官"
                    }
                }
                6 -> str = "七杀"
                7 -> {
                    str = if (riGanIndex % 2 == 1) {
                        "正官"
                    } else {
                        "正印"
                    }
                }
                8 -> str = "偏印"
                9 -> str = "正印"
            }
        }
        return str
    }

    /*
    * 获取藏干
    * @param dzIndex 地支索引
    */
    fun getDcang(dzIndex: Int): String {
        var arr = arrayOf("壬甲", "癸", "己癸辛", "甲丙戊", "乙", "戊乙癸", "丙戊庚", "丁己", "己丁乙", "庚壬戊", "辛", "戊辛丁")
        return arr[dzIndex];
    }


    /*
     * 获取地支藏干的十神
     * @author gougoushan@qq.com
     * @param dz 地支
     * @param rg 日干
     */
    fun getDzSS(dz:String, rg:String):String
    {
        val cgs = getDcang(dizhi.indexOf(dz))
        var ss = ""
        cgs.toCharArray().forEach {
            val cg = it.toString()
            ss += ssShorter[getShishen(tiangan.indexOf(cg), tiangan.indexOf(rg))]
        }
        return ss
    }

}
